<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Http\Services\RoomServices;
use App\Http\Services\RoomMsgServices;

class CommandKeepRongCloudRoom extends Command
{
    protected $signature = 'CommandKeepRongCloudRoom';

    protected $description = '保活融云聊天室';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        $services_room = new RoomServices();
        $room_msg_services = new RoomMsgServices();

        do {
            $queueKey = 'RONGCLOUD:KEEP:QUEUE';
            $roomId = Redis::RPOP($queueKey);
            if (empty($roomId)) {
                $roomIdArr = $services_room->getOpenedRoomIdArr();
                foreach ($roomIdArr as $roomId) {
                    Redis::LPUSH($queueKey, $roomId);
                }
            } else {
                $room_msg_services->keepRongCloud($roomId);
            }
            for ($i = 0; $i < 200; $i++) {
                $roomId = Redis::RPOP($queueKey);
                if (empty($roomId)) {
                    break;
                }
                $room_msg_services->keepRongCloud($roomId);
            }
        } while (false);
    }

}